package com.kiln.juxingqs.bestpayapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bestpay.app.PaymentTask;
import com.kiln.juxingqs.R;


public class BestpayResultActivity extends Activity {

    private TextView errcodeView;
    private Button bestpayResultBtn;
    private String license = "b41dfb110b7d5994b2f79ce0c025b94060c007eb3d04636335daa5c50e7b5463677b9d883a92f0a1fdaaf19e414b304a2ad7eb0ca6fe2c7d46b6724d90ce84655f00da570d64edb5cc0a6599056f9250655028032377d1ccc6936da69240d827d879debb2af75aae7ea829c7ac0424a196cb9bf5d044aa12c37fde27e6232fc9";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bestpay_result);
        bestpayResultBtn = findViewById(R.id.bestpay_result_confirm);
        errcodeView = findViewById(R.id.bestpay_result_code);
        bestpayResultBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Bundle b = getIntent().getExtras();
        String INSTITUTIONCODE = b.getString("INSTITUTIONCODE");
        String SIGN = b.getString("SIGN");
        String merchantNo = b.getString("merchantNo");
        String TRADENO = b.getString("TRADENO");
        String ORDERAMOUNT = b.getString("ORDERAMOUNT");
        String SUBJECT = b.getString("SUBJECT");
        String outTradeNo = b.getString("outTradeNo");

        final String paramsStr =
                "merchantNo=" + merchantNo
                        + "&institutionCode=" + INSTITUTIONCODE
                        + "&institutionType=" + "MERCHANT"
                        + "&signType=" + "CA"
                        + "&sign=" + SIGN
                        + "&platform=" + "android_4.0"
                        + "&tradeType=" + "acquiring"
                        + "&tradeNo=" + TRADENO
                        + "&outTradeNo=" + outTradeNo
                        + "&tradeAmt=" + ORDERAMOUNT
                        + "&tradeDesc=" + SUBJECT;
        Log.i("bestpayTag", "拉起翼支付收银台参数:" + paramsStr);
        Toast.makeText(getApplicationContext(), "准备拉起翼支付收银台", Toast.LENGTH_SHORT).show();
        PaymentTask paymentTask = new PaymentTask(this);

        paymentTask.pay(paramsStr, license);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i("intentTag", "进入BestpayResultActivity onNewIntent...");
        Bundle b = getIntent().getExtras();
        String INSTITUTIONCODE = b.getString("INSTITUTIONCODE");
        String SIGN = b.getString("SIGN");
        String merchantNo = b.getString("merchantNo");
        String TRADENO = b.getString("TRADENO");
        String ORDERAMOUNT = b.getString("ORDERAMOUNT");
        String SUBJECT = b.getString("SUBJECT");
        String outTradeNo = b.getString("outTradeNo");

        final String paramsStr =
                "merchantNo=" + merchantNo
                        + "&institutionCode=" + INSTITUTIONCODE
                        + "&institutionType=" + "MERCHANT"
                        + "&signType=" + "CA"
                        + "&sign=" + SIGN
                        + "&platform=" + "android_4.0"
                        + "&tradeType=" + "acquiring"
                        + "&tradeNo=" + TRADENO
                        + "&outTradeNo=" + outTradeNo
                        + "&tradeAmt=" + ORDERAMOUNT
                        + "&tradeDesc=" + SUBJECT;
        Log.i("bestpayTag", "拉起翼支付收银台参数:" + paramsStr);
        Toast.makeText(getApplicationContext(), "准备拉起翼支付收银台", Toast.LENGTH_SHORT).show();
        PaymentTask paymentTask = new PaymentTask(this);
        paymentTask.pay(paramsStr, license);
    }

    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {
        //翼支付收银台支付成功
        if (resultCode == -1) {
            Toast.makeText(this, "BestPay pay result: true", Toast.LENGTH_SHORT).show();
            errcodeView.setText("支付成功");
        } else {
            if (data != null) {
                Toast.makeText(this, "BestPay pay result: " + data.getStringExtra("result"),
                        Toast.LENGTH_SHORT).show();
                errcodeView.setText(data.getStringExtra("result"));
            } else {
                errcodeView.setText("支付失败");
                Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
            }

        }

    }
}
